<?php

namespace app\api\Logic\Goods;

use app\api\model\CampusGoodsModel;
use app\api\model\CampusGoodsSpecModel;
use app\api\model\CampusGoodsSpecNameModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class CampusGoodsLogic
{
    /**
     * 商品详情
     * @param $goodsId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function goodsInfo($goodsId)
    {
        $goodsInfo = CampusGoodsModel::where('id',$goodsId)
            ->field('id,name,price,stock')
            ->find();
        $goodsInfo = objToArray($goodsInfo) ?? [];

        //获取规格
        $spec = $this->spec($goodsId);
        $goodsInfo['spec_list'] = $spec["spec_list"];
        $goodsInfo['spec_value'] = $spec["spec_value"];

        return $goodsInfo;
    }

    /**
     * 获得商品规格信息
     * @param $goodsId
     * @return array[]
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    protected function spec($goodsId): array
    {

        $res = [
            "spec_list" => array(),
            "spec_value" => array(),
        ];

        // 获取规格
        $spec_list = CampusGoodsSpecModel::where('goods_id', $goodsId)
            ->field('price,cost_price,spec_id,stock,spec_name,spec_image')
            ->select();
        if (!empty($spec_list)) {

            $spec_ids = [];
            $specList = [];
            foreach ($spec_list as $val) {
                $temp_spec_id = explode("_", $val['spec_id']);
                for ($i = 0; $i < count($temp_spec_id); $i++) {
                    if (isset($spec_ids[$i])) {
                        if (!in_array($temp_spec_id[$i], $spec_ids[$i]))
                            $spec_ids[$i][] = $temp_spec_id[$i];
                    } else {
                        $spec_ids[$i][] = $temp_spec_id[$i];
                    }
                }
                $specList[$val['spec_id']] = $val;
            }
            $specs = [];
            for ($j = 0; $j < count($spec_ids); $j++) {
                $spec_name_list = CampusGoodsSpecNameModel::where('id', 'in', $spec_ids[$j])
                    ->field('id,key_name,value_name')
                    ->select();
                $specs[$j]["name"] = $spec_name_list[0]["key_name"];
                $specs[$j]["value"] = $spec_name_list;
            }
            $res = [
                "spec_list" => $specs,
                "spec_value" => $specList,
            ];
        }
        return $res;
    }

    /**
     * 根据规格ID获取规格名
     * @param $specId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getSpecNameBySpecId($specId){
        $info = CampusGoodsSpecNameModel::where('id',$specId)
            ->field('id,key_name,value_name')
            ->find();
        return objToArray($info) ?? [];
    }
}
